import 'package:flutter/material.dart';
import 'package:identification_photo/config/constants.dart';
import 'package:identification_photo/model/view_state_error.dart';
import 'package:identification_photo/utils/format_util.dart';

class BaseViewModel extends ChangeNotifier {
  ///当前页面状态
  ViewState _state = ViewState.busy;
  ///页面错误详细信息
  ViewStateError? _viewStateError;
  set viewState(ViewState state) {
    _state = state;
    notifyListeners();
  }

  bool isBusy() => _state == ViewState.busy;

  bool isEmpty() => _state == ViewState.empty;

  bool isError() => _state == ViewState.error;

  bool isIdly() => _state == ViewState.idle;

  void setIdle() => viewState = ViewState.idle;

  void setEmpty() => viewState = ViewState.empty;

  void setBusy() => viewState = ViewState.busy;

  void setError(e, stackTrace, {String? message}) {
    StateErrorType errorType =
        FormatUtil.dioErrorFormat(e).errorType ?? StateErrorType.defaultError;
    message = FormatUtil.dioErrorFormat(e).message ?? "出错鸟~~~";
    debugPrint(" errorType: $errorType, errorMsg: $message, errorInfo: ${e.toString()},$stackTrace");
    viewState = ViewState.error;
    _viewStateError = ViewStateError(
        errorType: errorType, errorMsg: message, errorInfo: e.toString());
  }
}
